package com.barneyx.mshinfoplatform.entity.admin;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.barneyx.mshinfoplatform.entity.CommonEntity;
import com.barneyx.mshinfoplatform.entity.area.AreaEntity;
import com.barneyx.mshinfoplatform.entity.enums.EUserState;
import com.barneyx.mshinfoplatform.entity.user.UserEntity;
import com.gitee.sunchenbin.mybatis.actable.annotation.*;
import com.gitee.sunchenbin.mybatis.actable.constants.MySqlCharsetConstant;
import com.gitee.sunchenbin.mybatis.actable.constants.MySqlEngineConstant;
import com.gitee.sunchenbin.mybatis.actable.constants.MySqlTypeConstant;
import io.swagger.annotations.ApiModel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@TableName("msh_admin_list")
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("管理员表")
@TableEngine(MySqlEngineConstant.InnoDB)
@TableCharset(MySqlCharsetConstant.UTF8MB4)
public class AdminUserEntity extends CommonEntity {
    @TableId(value = "admin_id", type = IdType.ASSIGN_ID)
    @Column(type = MySqlTypeConstant.BIGINT)
    private String AdminId;

    @TableField("user_id")
    @Column(type = MySqlTypeConstant.BIGINT)
    private String PUserId;

    @TableField("is_sys")
    @Column(type = MySqlTypeConstant.BIT, defaultValue = "0")
    private Boolean IsSys;

    @TableField(exist = false)
    private UserEntity User;

    @TableField("user_nickname")
    private String UserNickName;

    @TableField("user_email")
    @Column(type = MySqlTypeConstant.CHAR)
    @Unique
    @Index
    public String UserEmail;

    @TableField("user_name")
    @Unique
    @Index
    @NotBlank(message = "用户名称不能为空")
    private String UserName;

    @TableField("user_pwd")
    @NotBlank(message = "用户登陆密码不能为空")
    @Column(type = MySqlTypeConstant.CHAR, length = 32)
    private String UserPwd;

    @TableField("user_state")
    @Column(type = MySqlTypeConstant.TINYINT)
    private EUserState UserState;


    @TableField(exist = false)
    private List<AdminRoleEntity> Roles = new ArrayList<>();

    @TableField(exist = false)
    private List<String> RoleStrList = new ArrayList<>();

    @TableField(exist = false)
    private List<AdminPermissionEntity> Permissions = new ArrayList<>();

    @TableField(exist = false)
    private List<String> PermissionStrList = new ArrayList<>();

    @TableField(exist = false)
    private List<AreaEntity> ControllerAreas = new ArrayList<>();

    @TableField(exist = false)
    private List<String> ControllerAreaIds = new ArrayList<>();

    public void fullInfoStrInfo() {
        if (!Roles.isEmpty())
            this.RoleStrList = Roles.stream().map(AdminRoleEntity::getRoleCode).collect(Collectors.toList());
        if (!Permissions.isEmpty())
            this.PermissionStrList = Permissions.stream().map(AdminPermissionEntity::getPermissionCode).collect(Collectors.toList());
        if (!ControllerAreas.isEmpty())
            this.ControllerAreaIds = this.ControllerAreas.stream().map(AreaEntity::getAreaId).collect(Collectors.toList());
    }

    public void clearEntityInfoList() {
        this.Roles.clear();
        this.Permissions.clear();
//        this.ControllerAreas.clear();
        this.setUserPwd("");
    }
}
